package ntd.block.file;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.StaticLog;
import ntd.block.file.bo.DiskInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class NtdFile {

    public static List<DiskInfo> diskInfos = new ArrayList<>();

    public static void main(String[] args) {
        System.out.println(listRoots());
    }

    static {
        ThreadUtil.execAsync(() -> listRoots());
    }

    public static List<DiskInfo> listRoots() {
        List<File> files = ListUtil.toList(File.listRoots());
        diskInfos = CollStreamUtil.toList(files, file -> {
            DiskInfo diskInfo = new DiskInfo();
            diskInfo.setPath(file.getPath());
            diskInfo.setFreeSpace(file.getFreeSpace() / 1024 / 1024 + "M");
            diskInfo.setUsableSpace(file.getUsableSpace() / 1024 / 1024 + "M");
            diskInfo.setTotalSpace(file.getTotalSpace() / 1024 / 1024 + "M");
            return diskInfo;
        });
        StaticLog.info("NTD ------ 磁盘空间监测完成");
        return diskInfos;
    }
}
